//
//  HQNewProductDetailViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/7/19.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQNewProductDetailViewController.h"

@interface HQNewProductDetailViewController ()
{
    NSInteger _currentIdx;
    NSString *_imgUrl;
}
@end

@implementation HQNewProductDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商品详情";
    
    if(![self.pushType isEqualToString:@"我的收藏"]){

    }else{
//        _leftItem.hidden = YES;
//        _rightItem.hidden = YES;
    }
    
    _currentIdx = self.currentIndex;
    if(_productListArray.count == 1){
        _leftItem.hidden = YES;
        _rightItem.hidden = YES;
    }
    if(_currentIdx == 0){
        _leftItem.hidden = YES;
    }
    if(_currentIdx == _productListArray.count-1){
        _rightItem.hidden = YES;
    }
    
    _imgUrl = self.dataInfo[@"s_img"];
    _currentAID = self.dataInfo[@"a_id"];
    [self requestProductDetail];
}

/**
 收藏
 */
-(void)collectionItem{
    NSLog(@"点击了收藏");
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam addcoll:_currentAID uid:[UserModelManager userID]] withMethod:[HQMethod addcoll] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSLog(@"%@",sucData);
            [self tostView:@"收藏成功"];
            [self requestProductDetail];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 请求新品详情
 */
-(void)requestProductDetail{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam newproinfo:_currentAID withUid:[UserModelManager userID]] withMethod:[HQMethod newproinfo] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSDictionary *dic = sucData[@"data"];
            _imgUrl = dic[@"s_img"];
            [_productImage setImageWithURL:[NSURL URLWithString:dic[@"s_img"]] placeholderImage:[UIImage imageNamed:@"none_image"]];
            // 产品名称
            _cpmcLabel.text = [NSString stringWithFormat:@"  %@  ",dic[@"s_name"]];
            // 产品风格
            [self sourceLabel:_cpfgLabel withTitle:@"产品风格: " withData:dic[@"fengge"]];
            // 产品材质
            [self sourceLabel:_cpczLabel withTitle:@"产品材质: " withData:dic[@"caizhi"]];
            // 生产厂家
            [self sourceLabel:_sccjLabel withTitle:@"生产厂家: " withData:dic[@"username"]];
            // 产品卖点
            [self sourceLabel:_cpmdLabel withTitle:@"产品卖点: " withData:dic[@"peixu"]];
            // 产品价格
            [self sourceLabel:_jiage withTitle:@"￥" withData:dic[@"price"] withData2:@""];
            if(![self.pushType isEqualToString:@"我的收藏"]){
                if([dic[@"isgot"] integerValue] == 0){
                    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemImage:@"Collection2" withTarget:self withSelector:@selector(collectionItem) withSize:CGSizeMake(25, 25)];
                }else if( [dic[@"isgot"] integerValue] == 1){
                    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemImage:@"Collection1" withTarget:self withSelector:nil withSize:CGSizeMake(25, 25)];
                }
            }
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 上一个产品
 
 @param sender 按钮
 */
- (IBAction)leftItem:(id)sender {
    _currentIdx -- ;
    if(_currentIdx == 0){
        _leftItem.hidden = YES;
        _currentAID = _productListArray[_currentIdx][@"a_id"];
        [self requestProductDetail];
    }else{
        _currentAID = _productListArray[_currentIdx][@"a_id"];
        [self requestProductDetail];
        if(_productListArray.count > 1){
            _rightItem.hidden = NO;
        }
        _leftItem.hidden = NO;
    }
}

/**
 下一个产品
 
 @param sender 按钮
 */
- (IBAction)rightItem:(id)sender {
    _currentIdx ++ ;
    if(_currentIdx != _productListArray.count){
        _rightItem.hidden = NO;
        if(_currentIdx > 0){
            _leftItem.hidden = NO;
        }else{
            _leftItem.hidden = YES;
        }
        _currentAID = _productListArray[_currentIdx][@"a_id"];
        [self requestProductDetail];
    }else{
        _rightItem.hidden = YES;
    }
}

/**
 查看大图
 
 @param sender 查看大图
 */
- (IBAction)lookBigImage:(id)sender {
    HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
    bigImageVc.imageURL = _imgUrl;
    [self presentViewController:bigImageVc animated:YES completion:nil];
}

@end
